package com.briup.smartcity.service.impl;

import com.briup.smartcity.bean.Article;
import com.briup.smartcity.bean.extend.ArticleExtend;
import com.briup.smartcity.exception.ServiceException;
import com.briup.smartcity.mapper.ArticleMapper;
import com.briup.smartcity.mapper.extend.ArticleExtendMapper;
import com.briup.smartcity.service.IArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper mapper;
    @Autowired
    //使用扩展的Mapper来操作
    private ArticleExtendMapper extendMapper;

    @Override
    public ArticleExtend findByIdWithCategoryAndUser(int id) throws ServiceException {
       return extendMapper.selectArticleWithCategoryAndUser(id);

    }

    @Override
    public void saveOrUpdate(Article article) throws ServiceException {
        mapper.updateByPrimaryKeySelective(article);
    }

    @Override
    public PageInfo<ArticleExtend> pageQueryArticleWithCategoryAndUser(int pageNum, int pageSize, String title, Integer status, Integer categoryId) throws ServiceException {

        PageHelper.startPage(pageNum,pageSize,true);
        ArticleExtend articleExtend = new ArticleExtend();
        articleExtend.setArticleStatus(status);
        articleExtend.setArticleTitle(title);
        articleExtend.setArticleCategoryId(categoryId);
        List<ArticleExtend> articleExtendList = extendMapper.selectPageQueryArticleWithCategoryAndUser(articleExtend);
        PageInfo<ArticleExtend> pageInfo = new PageInfo<>(articleExtendList);

        return pageInfo;

    }

    @Override
    public void deleteById(int id) throws ServiceException {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ArticleExtend> pageQueryRecommend(int pageNum, int pageSize) throws ServiceException {
        return null;
    }

    @Override
    public void changeStatus(int id, int status) throws ServiceException {
        Article article = new Article();
        article.setArticleId(id);
        article.setArticleStatus(status);
        mapper.updateByPrimaryKeySelective(article);
    }
}
